package org.jupiter.blog.web.controller;

import jakarta.validation.Valid;
import org.jupiter.blog.api.common.TdesignTreeNode;
import org.jupiter.blog.api.dto.JupiterPermissionDTO;
import org.jupiter.blog.api.dto.JupiterPermissionTreeDTO;
import org.jupiter.blog.api.dto.PageResponseDto;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限管理控制器
 */
@RestController
@RequestMapping("/permissions")
public class JupiterPermissionController {

	@Autowired
	private JupiterPermissionService permissionService;

	/**
	 * 创建权限
	 */
	@PostMapping
	public JupiterResult<Void> createPermission(@RequestBody @Valid JupiterPermissionDTO dto) {
		permissionService.createPermissionWithUrls(dto);
		return JupiterResult.success(null);
	}

	/**
	 * 根据UUID获取权限
	 */
	@GetMapping("/{uuid}")
	public JupiterResult<JupiterPermissionDTO> getPermissionById(@PathVariable("uuid") String uuid) {
		return JupiterResult.success(permissionService.getById(uuid));
	}

	/**
	 * 更新权限
	 */
	@PutMapping("/{uuid}")
	public JupiterResult<Void> updatePermission(@PathVariable("uuid") String uuid,
			@RequestBody @Valid JupiterPermissionDTO dto) {
		permissionService.updatePermission(uuid, dto);
		return JupiterResult.success(null);
	}

	/**
	 * 删除权限
	 */
	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> deletePermission(@PathVariable("uuid") String uuid) {
		permissionService.deletePermission(uuid);
		return JupiterResult.success(null);
	}

	/**
	 * 恢复权限
	 */
	@RequestMapping(value = "/restorePermission/{uuid}", method = RequestMethod.POST)
	public JupiterResult<Void> restorePermission(@PathVariable("uuid") String uuid) {
		permissionService.restorePermission(uuid);
		return JupiterResult.success(null);
	}

	/**
	 * 分页查询权限列表
	 */
	@PostMapping("/page")
	public JupiterResult<PageResponseDto<JupiterPermissionDTO>> getPermissionsByPage(
			@RequestBody JupiterPermissionDTO queryDTO) {
		List<JupiterPermissionDTO> permissions = permissionService.getByPage(queryDTO);
		long total = permissionService.getCount(queryDTO);
		return JupiterResult.page(permissions, total);
	}

	/**
	 * 获取所有权限
	 */
	@GetMapping
	public JupiterResult<List<JupiterPermissionDTO>> getAllPermissions() {
		return JupiterResult.success(permissionService.getAll());
	}

	/**
	 * 获取权限树
	 */
	@GetMapping("/tree")
	public JupiterResult<List<TdesignTreeNode>> getPermissionTree() {
		return JupiterResult.success(permissionService.getTree());
	}

	/**
	 * 根据角色ID获取权限列表
	 */
	@GetMapping("/role/{roleId}")
	public JupiterResult<List<JupiterPermissionDTO>> getPermissionsByRoleId(@PathVariable("roleId") String roleId) {
		return JupiterResult.success(permissionService.getByRoleId(roleId));
	}

	/**
	 * 检查权限编码是否存在
	 */
	@GetMapping("/check/code")
	public JupiterResult<Boolean> checkPermissionCode(@RequestParam String code) {
		return JupiterResult.success(permissionService.existsByCode(code));
	}

	/**
	 * 为权限分配URL资源
	 */
	@PostMapping("/{uuid}/urls")
	public JupiterResult<Void> assignUrlsToPermission(@PathVariable("uuid") String uuid,
			@RequestBody List<String> urlResourceUuids) {
		permissionService.assignUrlsToPermission(uuid, urlResourceUuids);
		return JupiterResult.success(null);
	}

	/**
	 * 获取权限关联的URL资源列表
	 */
	@GetMapping("/{uuid}/urls")
	public JupiterResult<List<String>> getPermissionUrlResources(@PathVariable("uuid") String uuid) {
		return JupiterResult.success(permissionService.getPermissionUrlResources(uuid));
	}
}
